//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageIndex: 1,
    freeOrderList: [],
    // 
    uid: "",
    userType: "",
    // 
    top: [{
      'title': '收入',
      'active': true,
      'type': 0
    },
    {
      'title': '支出',
      'active': false,
      'type': 1,
    }
    ],
    orderType: 0,
    state: "",
    Type: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
      userType: wx.getStorageSync('userType')
    })

    this.FreePayOrderList();
  },

  // 顶部
  topTap: function (e) {
    var top = this.data.top;
    var Type = e.currentTarget.dataset.type;
    for (var i = 0, len = top.length; i < len; ++i) {
      if (top[i].type == Type) {
        top[i].active = true;
      } else {
        top[i].active = false
      }
    }
    this.setData({
      top: top,
      Type: Type
    })
    this.data.pageIndex = 1;
    this.data.freeOrderList = [];
    this.FreePayOrderList();
  },
  // 自由支付
  FreePayOrderList: function (e) {
    var that = this;
    var uid = this.data.uid;
    var pageIndex = this.data.pageIndex;
    var freeOrderList = this.data.freeOrderList;
    var Type = this.data.Type;
    if (pageIndex == 1) {
      wx.showLoading({
        title: '加载中',
      })
    }
    if (Type == '0') {
      wx.request({
        url: baseJs.api + baseJs.encryUrl("method=FreePayOrderList&SJID=" + uid + "&PageSize=10&PageIndex=" + pageIndex + "&OrderType=" + 0),
        success: function (res) {
          // console.log(JSON.parse(baseJs.decryUrl(res.data)));
          var json = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
          console.log(json)
          for (var i = 0; i < json.length; i++) {
            json[i].FinishTime = json[i].FinishTime.replace('T', ' ');
            freeOrderList.push(json[i])
          }
          that.setData({
            freeOrderList: freeOrderList
          })
          wx.hideLoading()
          // console.log(freeOrderList);
          if (json.length == 10) {
            that.setData({
              state: 'loadMore',
            })
          } else if (json.length == 0 && pageIndex == 1) {
            that.setData({
              state: 'noData',
            })
          } else if (json.length < 10) {
            that.setData({
              state: 'noMore',
            })
          } else if (json.length == 0 && pageIndex > 1) {
            that.setData({
              state: 'noMore',
            })
          }
        }
      })
    } else {
      wx.request({
        url: baseJs.api + baseJs.encryUrl("method=GetMyFreeOrderList&UID=" + uid + "&PageSize=10&PageIndex=" + pageIndex + "&OrderType=" + 0),
        success: function (res) {
          // console.log(JSON.parse(baseJs.decryUrl(res.data)));
          var json = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
          console.log(json)
          for (var i = 0; i < json.length; i++) {
            json[i].FinishTime = json[i].FinishTime.replace('T', ' ');
            freeOrderList.push(json[i])
          }
          that.setData({
            freeOrderList: freeOrderList
          })
          wx.hideLoading()
          // console.log(freeOrderList);
          if (json.length == 10) {
            that.setData({
              state: 'loadMore',
            })
          } else if (json.length == 0 && pageIndex == 1) {
            that.setData({
              state: 'noData',
            })
          } else if (json.length < 10) {
            that.setData({
              state: 'noMore',
            })
          } else if (json.length == 0 && pageIndex > 1) {
            that.setData({
              state: 'noMore',
            })
          }
        }
      })
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.data.pageIndex++;
    this.FreePayOrderList();
  },

})